<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="编号" prop="noNumber">
        <el-input
          v-model="queryParams.noNumber"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="客户" prop="customer">
        <el-input
          v-model="queryParams.customer"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库日期" prop="outDate">
        <el-date-picker clearable
          v-model="queryParams.outDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库日期" prop="inDate">
        <el-date-picker clearable
          v-model="queryParams.inDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="medium"
          @click="handleAdd"
          v-hasPermi="['system:zhang:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="medium"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:zhang:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="medium"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:zhang:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="medium"
          @click="handleExport"
          v-hasPermi="['system:zhang:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="medium"
          @click="handleCreateFrom"
          v-hasPermi="['system:zhang:export']">生成单据</el-button>
      </el-col>
      <el-col :span="1.5">
      <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="revoke"
          v-hasPermi="['system:zhang:export']">撤销生成</el-button> -->
<!--      <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click=""-->
<!--          v-hasPermi="['system:zhang:export']">单据后仓库合计</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zhangList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="编号" align="center" prop="noNumber" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="地址" align="center" prop="addres" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="客户" align="center" prop="customer" />
      <el-table-column label="最近出库日期" align="center" prop="outDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库日期" align="center" prop="inDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="allPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:zhang:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:zhang:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改zhang对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="70px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编号" prop="noNumber">
          <el-input v-model="form.noNumber" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
<!--        <el-form-item label="是否生成了单据0-否 1-是" prop="isRecord">-->
<!--          <el-input v-model="form.isRecord" placeholder="请输入是否生成了单据0-否 1-是" />-->
<!--        </el-form-item>-->
        <el-form-item label="地址" prop="addres">
          <el-input v-model="form.addres" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="客户" prop="customer">
          <el-input v-model="form.customer" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="出库日期" prop="outDate">
          <el-date-picker clearable
            v-model="form.outDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出库日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库日期" prop="inDate">
          <el-date-picker clearable
            v-model="form.inDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入库日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总价" prop="allPrice">
          <el-input v-model="form.allPrice" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    选中商品按钮-->
    <el-dialog :title="title"  :visible.sync="slite" width="800px" append-to-body>
      <el-table v-loading="loading" :data="zhangList" @selection-change="handleSelectionChange">
        <el-table-column type="selection"  width="55" align="center" />
<!--        <el-table-column label="商品id" align="center" prop="id" />-->
        <el-table-column label="名称" align="center" prop="name" />
<!--        <el-table-column label="编号" align="center" prop="noNumber" />-->
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="地址" align="center" prop="addres" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="number">
          <template #default="scope">
            <el-input @input="getNumber(scope.row.number,form1[scope.$index],scope.$index)"  v-model="form1[scope.$index]"     placeholder="请输入数量" />
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" prop="price" />
        <el-table-column label="客户" align="center" prop="customer" />
<!--        <el-table-column label="出库日期" align="center" prop="outDate" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ parseTime(scope.row.outDate, '{y}-{m}-{d}') }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="入库日期" align="center" prop="inDate" width="180">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ parseTime(scope.row.inDate, '{y}-{m}-{d}') }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="总价" align="center" prop="allPrice" />
<!--        <el-table-column label="备注" align="center" prop="remark" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:zhang:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:zhang:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <span style="margin-right: 30%;">合计：{{ totals }}</span>
        <el-button type="primary" @click="verify">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZhang, getZhang, delZhang, addZhang, updateZhang ,addCreatZhang} from "@/api/system/zhang";
import {listHistory,getHistory,delHistory,addHistory,updateHistory} from "@/api/system/history";
import { createLogger } from "vuex";

export default {
  name: "product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // zhang表格数据
      zhangList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹外框
      slite: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        noNumber: null,
        specification: null,
        addres: null,
        unit: null,
        number: null,
        price: null,
        customer: null,
        outDate: null,
        inDate: null,
        allPrice: null,
      },
      // 表单参数
      form: {},
      //表达
      form1:[],
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  computed:{
    totals(){
      let count = 0
      for(let i=0;i<this.zhangList.length;i++){
        for(let j=0;j<this.ids.length;j++){
          if(this.zhangList[i].id == this.ids[j]){
            console.log(this.zhangList[i].price+","+this.form1[this.zhangList[i].id-1])
            count +=  this.zhangList[i].price * this.form1[this.zhangList[i].id-1]
          }
        }
        
      }
      if(!count){
        count = 0
      }
      return count
    }
  },
  methods: {
    /** 查询zhang列表 */
    getList() {
      this.loading = true;
      listZhang(this.queryParams).then(response => {
        this.zhangList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    close(){
      this.slite = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        noNumber: null,
        specification: null,
        isRecord: null,
        addres: null,
        unit: null,
        number: null,
        price: null,
        customer: null,
        outDate: null,
        inDate: null,
        allPrice: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAllQuery(){
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {

      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getZhang(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateZhang(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZhang(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除上班编号为"' + ids + '"的数据项？').then(function() {
        return delZhang(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/zhang/export', {
        ...this.queryParams
      }, `商品单据.xlsx`)

    },
    /** 生成单据按钮操作  3s */
    handleCreateFrom(){
      this.reset();
      this.slite=true;
      this.title="选择商品，并选择数量";
      // this.$refs["form"].validate(valid => {
      //   console.log(valid)
      //   if (valid) {
      //     if (this.form.id != null) {
      //       addZhang(this.form).then(response => {
      //         this.$modal.msgSuccess("单据生成成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    //获取当前数量
    getNumber(maxValue,value,index){
     
      if(value>maxValue){
        this.$message.error('最大值为'+maxValue);
        this.form1[index] = maxValue
      }
    },
    //撤销生成
    revoke(){

    },




    verify(){
      // let datas = [] 
      if(this.totals){
        let ProductHistory = {
        listClass:"",
        number:'',
          effective: null,
          createTime: null,
        } 
      ProductHistory.listClass = this.ids.sort(function(a,b){
			return a - b;
		}).toString()
      for(let i=0;i<this.ids.length;i++){
        if(this.form1[this.ids[i]-1]){
           ProductHistory.number +=this.form1[this.ids[i]-1]+','
        }
         
        
      }

      ProductHistory.number = ProductHistory.number.substring(0, ProductHistory.number.lastIndexOf(','))
    
      addCreatZhang(ProductHistory).then((resolved) => {
              this.$modal.msgSuccess("单据生成成功");
              this.slite=false;
              this.open = false;
              this.getList();
              this.download('system/zhang/exportProduct', {
        ...this.queryParams
      }, `商品单据.xlsx`)
      }, (rejected) => {
        console.log("失败",rejected);
      })
      }else {
        this.$message.error('请勾选商品或勾选的商品数量为0');
      }
      

      
    }
  }
};
</script>
